package com.yangl.controller;

import com.yangl.domain.ResultWrapper;
import com.yangl.domain.ResultWrapperEnum;
import com.yangl.domain.User;
import com.yangl.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by lyang on 2016/10/19.
 */
@Controller
public class ProtocolController {
    @Resource
    private UserService userService;

    /*
    *获取指定用户
    */
    @RequestMapping("getUserProtocol")
    public
    @ResponseBody
    ResultWrapper getUserProtocol(int userId) {
        System.out.println("use getUserProtocol:");
        User user = userService.selectUserById(userId);
        if (user != null) {
            return new ResultWrapper(ResultWrapperEnum.SUCCESS, user);
        } else {
            return new ResultWrapper(ResultWrapperEnum.LIST_FAILURE);
        }

    }

    /*
*获取指定用户
*/
    @RequestMapping("getUserProtocol1")
    public
    @ResponseBody
    ResultWrapper getUserProtocol(String userId) {
        System.out.println("use getUserProtocol:");
        User user = userService.selectUserById(Integer.valueOf(userId).intValue());
        if (user != null) {
            return new ResultWrapper(ResultWrapperEnum.SUCCESS, user);
        } else {
            return new ResultWrapper(ResultWrapperEnum.LIST_FAILURE);
        }

    }

    /*
    *获取所有用户
    */
    @RequestMapping("getUserAllProtocol")
    public
    @ResponseBody
    ResultWrapper getUserAllProtocol() {
        return new ResultWrapper(ResultWrapperEnum.SUCCESS, userService.selectUserAll());
    }

    /*
    *新增用户
    */
    @RequestMapping("addUserProtocol")
    public
    @ResponseBody
    ResultWrapper addUserProtocol(String userName, String userPassword) {
        User user = new User(userName, userPassword);
        userService.addUser(user);
        return new ResultWrapper(ResultWrapperEnum.SUCCESS);
    }

    /*
    *修改用户
    */
    @RequestMapping("modifyUserProtocol")
    public
    @ResponseBody
    ResultWrapper modifyUserProtocol(int userId, String userName, String userPassword) {
        User user = new User(userId, userName, userPassword);
        if (userService.update(user)) {
            return new ResultWrapper(ResultWrapperEnum.SUCCESS);
        } else {
            return new ResultWrapper(ResultWrapperEnum.UPDATE_FAILURE);
        }
    }

    /*
    *删除用户
    */
    @RequestMapping("delUserProtocol")
    public
    @ResponseBody
    ResultWrapper delUserProtocol(int userId) {
        if (userService.deleteUserById(userId)) {
            return new ResultWrapper(ResultWrapperEnum.SUCCESS);
        } else {
            return new ResultWrapper(ResultWrapperEnum.DEL_FAILURE);
        }
    }
}